﻿Imports System.IO
Imports Microsoft.Win32

Public Class Resetwindowsprofile
    Public Shared Sub Resetwin(ByVal strcomp As String, ByVal struser As String)
        If UCase(Currentusr.CheckCurrentUser(strcomp)) = UCase(struser) Then
            Dim result2 As DialogResult =
                    MessageBox.Show(
                        struser & " is currently logged onto " & strcomp & " would you like to log them off?", "",
                        MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If result2 = DialogResult.Yes Then
                Dim t As Single
                Dim strObject As String
                Dim objComputer As Object
                strObject = "winmgmts://" & strcomp
                For Each objComputer In GetObject(strObject).InstancesOf("Win32_OperatingSystem")
                    t = objComputer.Win32Shutdown(0, 0)
                    ' ReSharper disable once CompareOfFloatsByEqualityOperator
                    If t <> 0 Then
                        'MsgBox("Error occurred!!!")
                    Else
                        Do While Not (Currentusr.CheckCurrentUser(strcomp) = "N/A")

                        Loop
                    End If
                Next
            ElseIf result2 = DialogResult.No Then

            End If
        End If
        'MessageBox.Show("user logged off")

        Dim objReg As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strcomp)
        Dim objRegKey As RegistryKey
        Dim objsubkey As RegistryKey
        Dim lstprofiles = New List(Of String)
        objRegKey = objReg.OpenSubKey("Software\Microsoft\Windows NT\currentVersion\ProfileList")
        For Each subkey In objRegKey.GetSubKeyNames
            objsubkey = objRegKey.OpenSubKey(subkey.ToString)
            If objsubkey.GetValue("ProfileImagePath").ToString.Contains(struser) Then
                lstprofiles.Add(objsubkey.GetValue("ProfileImagePath").ToString)
            End If
            objsubkey.Close()
        Next

        objRegKey.Close()
        'If lstprofiles.Count > 1 Then
        '    For Each strprofilepath In lstprofiles
        '        frmListOfProfiles.lblistofprofiles.Items.Add(strprofilepath)
        '    Next
        '    frmListOfProfiles.lblcompname.Text = strcomp
        '    frmListOfProfiles.Visible = True
        'Else
        '    For Each strprofilepath In lstprofiles
        '        windelfolder(strcomp, strprofilepath)
        '    Next
        'End If
    End Sub

    Public Shared Sub Windelfolder(ByVal strcomp As String, ByVal strpath As String)
        Dim strnewpath As String = "\\" & strcomp & "\" & strpath.Replace(":", "$") & "_old"
        MessageBox.Show("Moving " & strpath & " to " & strpath & "_old on " & strcomp)
        If Directory.Exists(strpath) And (Not Directory.Exists(strnewpath)) Then
            Directory.Move(strpath, strnewpath)
            Dim objReg As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strcomp)
            Dim objRegKey As RegistryKey
            Dim objsubkey As RegistryKey
            objRegKey = objReg.OpenSubKey("Software\Microsoft\Windows NT\currentVersion\ProfileList")
            For Each subkey In objRegKey.GetSubKeyNames
                objsubkey = objRegKey.OpenSubKey(subkey.ToString)
                If objsubkey.GetValue("ProfileImagePath").ToString.Equals(strpath) Then
                    objsubkey.Close()
                    objRegKey.DeleteSubKeyTree(subkey.ToString)
                End If
            Next
            objRegKey.Close()
            MessageBox.Show(
                "Profile Path " & Chr(34) & strpath & Chr(34) & " on " & Chr(34) & strcomp & Chr(34) &
                " has been moved to " & Chr(34) & strpath & "_old" & Chr(34) & " on " & strcomp)
        Else
            MessageBox.Show(
                "Either " & strpath & " does not exist on " & strcomp &
                " or there is an existing _old profile for this user, please manually compelte this task.")
        End If
    End Sub
End Class